package kr.saeildamil.androidstudy_second.clip;

import kr.saeildamil.andrordstudy.second.R;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.net.Uri;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.View;
import android.view.View.OnDragListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CopyText extends Activity {
	EditText copyedit;
	TextView pasteText;
	ClipboardManager cm;
	
	Button btnSourceText;
	Button btnSourceIntent;
	Button btnTargetText;
	Button btnTargerIntent;
	
	Button lBtn;
	Button rBtn;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch27_copytext);
		
		copyedit = (EditText) findViewById(R.id.copyedit);
		pasteText =  (TextView) findViewById(R.id.pastetext);
		cm =  (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
		
		
		btnSourceText = (Button) findViewById(R.id.source);
		btnSourceIntent = (Button) findViewById(R.id.source2);
		btnSourceText.setOnLongClickListener(longListener);
		btnSourceIntent.setOnLongClickListener(longListener);
		
		btnTargerIntent = (Button) findViewById(R.id.targetintent);
		btnTargerIntent.setOnDragListener(mDragListener);
		
		lBtn = (Button) findViewById(R.id.shadow_source);
		lBtn.setOnLongClickListener(longListener);
		
		rBtn = (Button) findViewById(R.id.shadow_target);
		rBtn.setOnDragListener(mDragListener);
		
	}
	
	
	
	View.OnDragListener mDragListener = new OnDragListener() {
		public boolean onDrag(View v, DragEvent event) {
			Button btn;
			if (v instanceof Button){
				btn = (Button) v;
			}else{
				return false;
			}
			switch(event.getAction()){
			case DragEvent.ACTION_DRAG_STARTED:
				if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)){
					btn.setText("Drop OK");
				}else if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_INTENT)){
					btn.setText("Drop OK Intent");
				}else{
					return false;
				}
				
				break;
			case DragEvent.ACTION_DRAG_ENTERED:
				btn.setText("Enter");
				break;
			case DragEvent.ACTION_DRAG_EXITED:
				btn.setText("Exit");
				break;
			case DragEvent.ACTION_DROP:
				if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)){
					String text = event.getClipData().getItemAt(0).getText().toString();
					btn.setText(text);
				}else if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_INTENT)){
					Intent intent = event.getClipData().getItemAt(0).getIntent();
					startActivity(intent);
					btn.setText("네이버");
				}
				break;
			case DragEvent.ACTION_DRAG_ENDED:
				if (event.getResult()){
					Toast.makeText(CopyText.this, "Drag & Drop Complete", Toast.LENGTH_LONG).show();
				}else{
					btn.setText("Target");
				}
				break;
			}
			return true;
		}
	};
	
	View.OnLongClickListener longListener = new OnLongClickListener() {
		public boolean onLongClick(View v) {
			ClipData clip;
			switch(v.getId()){
			case R.id.source:
				clip = ClipData.newPlainText("dragText", "DragText");
				v.startDrag(clip, new View.DragShadowBuilder(v), null, 0);
				break;
			case R.id.source2:
				clip = ClipData.newIntent("intent", new Intent(Intent.ACTION_VIEW,Uri.parse("http://m.naver.com")));
				v.startDrag(clip, new View.DragShadowBuilder(v), null, 0);
				break;
			case R.id.shadow_source:
				clip = ClipData.newPlainText("dragText", "DragText");
				v.startDrag(clip, new CanvasShadow(v), null, 0);
				break;
			}
			return false;
		}
	};
	
	public void mOnClick(View v){
		switch(v.getId()){
		case R.id.btncopy:
			copyText();
			break;
		case R.id.btnpaste:
			pasteText();
			break;
		case R.id.btncopyintent:
			copyIntent();
			break;
		case R.id.btnpasteintent:
			pasteIntent();
			break;
		}
	}

	private void copyIntent() {
		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.naver.com"));
		ClipData clip = ClipData.newIntent("intent", intent);
		cm.setPrimaryClip(clip);
		Toast.makeText(this, "Intent Copied", Toast.LENGTH_LONG).show();
	}

	private void pasteIntent() {
		if (!cm.hasPrimaryClip()){
			Toast.makeText(this, "Clipboard Empty", Toast.LENGTH_LONG).show();
			return;
		}
		if (!cm.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_INTENT)){
			Toast.makeText(this, "Clipboard has not Intent", Toast.LENGTH_LONG).show();
			return;
		}
		
		ClipData clip = cm.getPrimaryClip();
		ClipData.Item item = clip.getItemAt(0);
		Intent intent = item.getIntent();
		if (intent != null){
			startActivity(intent);
		}
	}

	private void copyText() {
		String str = copyedit.getText().toString();
		if (str.length() != 0){
			ClipData clip = ClipData.newPlainText("text", str);
			cm.setPrimaryClip(clip);
			Toast.makeText(this, "Text Copied", Toast.LENGTH_LONG).show();
		}
	}

	private void pasteText() {
		if (!cm.hasPrimaryClip()){
			Toast.makeText(this, "Clipboard Empty", Toast.LENGTH_LONG).show();
			return;
		}
		if (!cm.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)){
			Toast.makeText(this, "Clipboard has not text", Toast.LENGTH_LONG).show();
			return;
		}
		
		ClipData data = cm.getPrimaryClip();
		ClipData.Item item = data.getItemAt(0);
		pasteText.setText(item.getText());
		
	}
}


class CanvasShadow extends View.DragShadowBuilder{
	int mWidth;
	int mHeight;
	Paint pnt;
	Paint pnt2;
	
	public CanvasShadow(View v) {
		super(v);
		mWidth = v.getWidth();
		mHeight = v.getHeight();
		init();
	}

	private void init() {
		pnt = new Paint();
		pnt.setColor(Color.YELLOW);
		
		pnt2 = new Paint(Paint.ANTI_ALIAS_FLAG);
		pnt2.setColor(Color.RED);
		pnt2.setStrokeWidth(8);
		pnt2.setStyle(Paint.Style.STROKE);
	}

	@Override
	public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
		super.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
		shadowSize.set(mWidth, mHeight);
		shadowTouchPoint.set(mWidth/3,mHeight/3);
	}

	@Override
	public void onDrawShadow(Canvas canvas) {
		super.onDrawShadow(canvas);
		canvas.drawRect(0, 0, mWidth, mHeight, pnt);
		canvas.drawCircle(mWidth/2, mHeight/2, mHeight/2-5, pnt2);
	}	
}//end of class CAnvasShadow



















